import { getAuthMp, getAuthMa, getMaDetail } from '@/api/wechat'

const user = {
  state: {
    // mpLoad:false,
    // maLoad:false,
    // mp: null, // 公众号授权信息
    // ma: null, // 小程序授权信息
    // maInfo.DEFAULT.appId: null,// 当前激活的小程序Appid
    // maInfo.DEFAULT.isOnline: null, // 当前激活的小程序是否上线 =0为没上线
    maInfo: {
      DEFAULT: {
        info: null,
        auth: null,
        appId: null,
        isOnline: null
      },
      PAY_ASSISTANT: {
        info: null,
        auth: null
      }
    },
    mpInfo: {
      // defaultMpAuth: null,
      // defaultMpAppid: null,
      default: {
        auth: null,
        appId: null
      }
    }
  },

  mutations: {
    SET_MA_INFO: (state, { classifyCode, data }) => {
      switch (classifyCode) {
        case 'DEFAULT':
          state.maInfo.DEFAULT.info = data
          break
        case 'PAY_ASSISTANT':
          state.maInfo.PAY_ASSISTANT.info = data
          break
      }
    },
    SET_MA_AUTH: (state, { classifyCode, data }) => {
      switch (classifyCode) {
        case 'DEFAULT':
          state.maInfo.DEFAULT.auth = data
          state.maInfo.DEFAULT.appId = (data && data.base.appId) || null
          state.maInfo.DEFAULT.isOnline =
            (data && data.base.onlineVersionId) || 0
          break
        case 'PAY_ASSISTANT':
          state.maInfo.PAY_ASSISTANT.auth = data
          break
      }
    },
    SET_MP_AUTH: (state, { data }) => {
      state.mpInfo.default.auth = data
      state.mpInfo.default.appId = (data && data.base.appId) || null
    }
  },

  actions: {
    // 获取公众号授权信息
    getAuthMp({ commit }, data) {
      return new Promise((resolve, reject) => {
        data = data || { classifyCode: 'DEFAULT', scope: '' }
        const { classifyCode = 'DEFAULT', scope = '' } = data
        getAuthMp({ classifyCode, scope })
          .then((response) => {
            const data = response.data
            commit('SET_MP_AUTH', { classifyCode, data })
            resolve(data)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 获取小程序授权信息
    getAuthMa({ commit }, data) {
      return new Promise((resolve, reject) => {
        data = data || { classifyCode: 'DEFAULT', scope: '' }
        const { classifyCode = 'DEFAULT', scope = '' } = data
        getAuthMa({ classifyCode, scope })
          .then((response) => {
            const data = response.data
            commit('SET_MA_AUTH', { classifyCode, data })
            resolve(data)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 获取小程序详情
    getMaDetail({ commit }, data) {
      return new Promise((resolve, reject) => {
        data = data || { classifyCode: 'DEFAULT', scope: '' }
        const { classifyCode = 'DEFAULT', scope = '' } = data
        getMaDetail({ classifyCode, scope })
          .then((response) => {
            const data = response.data
            commit('SET_MA_INFO', { classifyCode, data })
            resolve(data)
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  }
}

export default user
